﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using MapTeams.Umbraco.Polls.Dto;
using MapTeams.Umbraco.Polls.Services;

namespace MapTeams.Umbraco.Polls.UI
{
    /// <summary>
    /// Web services to handle ajax request from the Poll widget + admin pages.
    /// 
    /// Author: 
    /// Indra Kurniawan
    /// MAP Teams (http://www.mapits.com)
    /// 
    /// functionality was refactored out from original design by Anz (www.ClientSideAsp.net).
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class PollsWebServices : System.Web.Services.WebService
    {

        /// <summary>
        /// Page method to call from JQuery AJAX to delete the Choice
        /// </summary>
        /// <param name="cID"></param>
        [WebMethod]
        public int DeletePollChoice(int cID)
        {
            PollsServiceLayer objPoll = new PollsServiceLayer(); //todo: use factory design pattern later //inject dependency from webConfig
            return objPoll.DeleteChoice(cID);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public MapTeamsPolls UpdatePollCount(int pID, int cID)
        {
            //update the vote count of the selected choice
            PollsServiceLayer objPoll = new PollsServiceLayer(); //todo: use factory design pattern later //inject dependency from webConfig
            objPoll.UpdateChoiceVote(cID);

            //save the users IP address - to block repeated vote if the BlockMode is by IP address
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            objPoll.InsertPollIP(pID, ip);

            //Save the poll ID in cookie - to block repeated vote if the BlockMode is by Cookie
            HttpCookie pollCookie;// = new HttpCookie(Constants.CookieName);
            string valueToStore = ""; //we store the poll IDs as comma seperated values

            //the cookie already exists and some PollIDs are already there
            if (HttpContext.Current.Request.Cookies[Constants.CookieName] != null && HttpContext.Current.Request.Cookies[Constants.CookieName]["ID"] != null)
            {
                pollCookie = HttpContext.Current.Request.Cookies[Constants.CookieName];
                valueToStore = HttpContext.Current.Request.Cookies[Constants.CookieName]["ID"] + "," + pID.ToString(); //append the current PollID to the already existing Poll IDs after a comma
            }
            else //cookie not exists - create a new one and store the ID
            {
                pollCookie = new HttpCookie(Constants.CookieName);
                valueToStore = pID.ToString();
            }
            pollCookie.Values["ID"] = valueToStore;
            pollCookie.Expires = DateTime.MaxValue; //this cookie will never expire
            HttpContext.Current.Response.Cookies.Add(pollCookie);

            //get the poll result
            DataSet dsPoll = objPoll.SelectPoll(pID);

            //translate the dataset to MapTeamsPolls dataStructure and send it to client 
            MapTeamsPolls pollInfo = PollsUtils.BuildPoll(dsPoll);
            pollInfo.IsAlreadyVoted = true; // just voted; set manually

            return pollInfo;
        }
    }
}
